'''
文档目的：测试绘制柱状图和折线图
遗留问题：
    1、标签在柱状图居中显示

'''


import  matplotlib.pyplot as plt
import matplotlib.spines as sp
import numpy as np

X = [2,3,4]
Y_Yellow = [10,20,30]
Y_Green = [1,3,5]
Y_Jiejuelv =[0.1,0.2,0.25]


''' **************基础设置：创建绘图区域，设置范围********************'''
# 创建双轴
fig,ax1 = plt.subplots()
ax2 = ax1.twinx()

# 设置x/y轴刻度范围
x_min = 1
x_max = 10

ax1.axis([x_min,x_max,0,50])
ax2.axis([x_min,x_max,0,1])

# x轴 标签设置
ax1.set_xticks([2, 3, 4])
ax1.set_xticklabels(["KW02", "KW03", "KW04"])

# y轴 标签设置
ax2.set_yticks([0,0.2, 0.4, 0.6,0.8,1])
ax2.set_yticklabels(["0","20%", "40%", "60%","80%","100%"])

# 顶部坐标轴，不适用，设置为无色
ax1.spines['top'].set_color("none")
ax2.spines['top'].set_color("none")

#*************************问题黄灯、绿灯柱状图绘制*********************************
#绘制柱
ax1.bar(X,Y_Green,color = "lime")
ax1.bar(X,Y_Yellow,bottom=Y_Green,color = "yellow")



#添加标签
for x,y in zip(X,Y_Green):
    ax1.text(x,y/2-0.4,y,ha='center', va='baseline')

for x,y_green,y_yellow in zip(X,Y_Green,Y_Yellow):
    ax1.text(x,y_green+y_yellow/2,y_yellow,ha='center',va = 'baseline')



#***************************** 问题解决率 ***************************************
#绘制图形
ax2.plot(X,Y_Jiejuelv)

# ax2.ylim((0,1))
#添加标签
for x,y in zip(X,Y_Jiejuelv):
    ax2.text(x,y,format(y,'.1%'),ha='left',va = 'baseline')





plt.show()